package net.sxnic.item.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sxnic.item.ucenter.UcenterService;
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.menu.Menu;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Main extends UgrActionSupport {


	@Autowired
	private UcenterService ucenterService;

	/**
	 * 一级菜单列表
	 */
	private List<Menu> menuList;

	/**
	 * 二级菜单Map
	 */
	private Map<String, List<Menu>> secodeMenuMap;

	/**
	 * 用户拥有权限的菜单map
	 */
	private Map<String, String> userMenuMap;

	/**
	 * 首页框架
	 * 
	 */
	private String indexPage;

	@RequestMapping(value = "/main.jspa", method = RequestMethod.GET)
	public String execute(ModelMap model) throws Exception {

		try {			
			userMenuMap = new HashMap<String, String>();
			userMenuMap = ucenterService
					.findUserMenus(AppContext.getUserName(request));

			menuList = new ArrayList<Menu>();
			List<Menu> TopMenuList = ucenterService.getTopLevelMenus();
			for (Menu m : TopMenuList) {
				if (userMenuMap.containsKey(m.getCode())) {
					menuList.add(m);
				}
			}
			
			secodeMenuMap = new HashMap<String, List<Menu>>();

			for (Menu m : menuList) {
				secodeMenuMap.put(m.getCode(),
						ucenterService.getSubLevel(m.getCode()));
			}
			
			
			indexPage = (StringUtils.isBlank(AppContext.getCssStyle(request))) ? "vistaBlue"
					: AppContext.getCssStyle(request);
//			indexPage = "sky";
			
			model.addAttribute("userMenuMap", userMenuMap);
			model.addAttribute("menuList", menuList);
			model.addAttribute("secodeMenuMap", secodeMenuMap);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "main/"+indexPage+"/main";
	}

	public String getIndexPage() {
		return indexPage;
	}

	public void setIndexPage(String indexPage) {
		this.indexPage = indexPage;
	}

	public Map<String, String> getUserMenuMap() {
		return userMenuMap;
	}

	public void setUserMenuMap(Map<String, String> userMenuMap) {
		this.userMenuMap = userMenuMap;
	}

	public Map<String, List<Menu>> getSecodeMenuMap() {
		return secodeMenuMap;
	}

	public void setSecodeMenuMap(Map<String, List<Menu>> secodeMenuMap) {
		this.secodeMenuMap = secodeMenuMap;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}

}
